package com.eques.base

import android.content.ComponentName
import android.content.Intent
import android.content.ServiceConnection
import android.os.Bundle
import android.os.IBinder
import androidx.appcompat.app.AppCompatActivity
import com.eques.icvss.utils.ELog
import com.eques.service.DoorBellService
import com.eques.service.DoorBellService.DoorBellBinder

open class BaseServiceActivity : AppCompatActivity() {
    private val TAG = BaseServiceActivity::class.java.simpleName
    protected  var doorBellService: DoorBellService?=null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        bindService()
    }

    /**
     * service创建成功回调接口
     */
    var connection: ServiceConnection = object : ServiceConnection {
        override fun onServiceDisconnected(arg: ComponentName) {
            ELog.d(TAG, " BaseServiceActivity ServiceConnection onServiceDisconnected start... ")
        }

        override fun onServiceConnected(arg: ComponentName, iBinder: IBinder) {
            ELog.d(TAG, " BaseServiceActivity ServiceConnection onServiceConnected start... ")
            val doorBellBinder = iBinder as DoorBellBinder
            doorBellService = doorBellBinder.service

            ELog.d(TAG, " BaseServiceActivity ServiceConnection onServiceConnected start...$doorBellService ")
        }
    }

    fun bindService() {
        bindService(
            Intent(this@BaseServiceActivity, DoorBellService::class.java),
            connection,
            BIND_AUTO_CREATE
        )
    }

    fun unBindService() {
        unbindService(connection)
    }

    override fun onDestroy() {
        super.onDestroy()
        unBindService()
    }


}
